****************************************************************************
* File-Nale: 		crossnational.do
* Date:		 04/30/2018
* Author: 		Fred Batista
* Purpose: 		Two-step analyses of gender gap in political knowledge
* Data used: 		crossnational.dta
* Data Output:	Journal of Politics� paper	*/
****************************************************************************


*** Seting up edvreg

program define edvreg
version 6.0
tempvar svar dvv varCorr
syntax varlist [if] [in], Dvste(string) [Prop] [borjas] [cluster(varlist)] [hc3]   [Level(integer 95)]
local indvars "`varlist'"
if "`cluster'"~="" {
  local cluster="cluster(`cluster')"
}
gettoken depvar indvars : indvars
qui gen `dvv' = `dvste'*`dvste' `if'`in'
disp
disp in green "Sampled dependent variable regression"
disp
if ("`prop'" == "") {
      qui gen `svar' = sum(`dvv')
      local swsq = `svar'[_N]
      qui reg `depvar' `indvars' `if'`in', level(`level') `cluster' `hc3' 
      local dof = e(df_r)
      local sumsr = `dof'*e(rmse)*e(rmse)
      qui matrix accum XpX  = `indvars' `if' `in'
      qui matrix accum XpGX = `indvars' [iw=`dvv'] `if' `in'
      matrix tr = trace(inv(XpX)*XpGX)

      *following line modified by E. leoni
      local sigma2 = (`sumsr'-`swsq') / `dof'      
      if ("`borjas'"=="") {
        local sigma2 = (`sumsr'-`swsq'+tr[1,1]) / `dof' 
      }
      if (`sigma2' < 0) {local sigma2 = 0}
      disp in green "Estimated Sigma     = " in yellow %7.5f sqrt(`sigma2')
      disp in green "Mean Omega2         = " in yellow %7.5f sqrt(`swsq'/`dof')
      qui gen `varCorr' = (1/(`sigma2'+`dvv')) `if'`in'
        regress `depvar' `indvars'  `if'`in' [aw=`varCorr'],  `cluster' `hc3' level(`level')
    }
   else {
     tempvar e v
      qui reg `depvar' `indvars'  `if'`in', level(`level') `cluster' `hc3'
      predict `e' if e(sample), resid
      replace `e' = `e'*`e'
      qui reg `e' `dvv' `if'`in',  level(`level') `cluster' `hc3'
      if (_b[_cons]>0) {
         disp in green "Coeff. on variance from sampling = " _continue
         disp in yellow %7.4f _b[`dvv'] " (" %7.4f _se[`dvv'] ")"
         disp in green "   Estimated variance of epsilon = " _continue
         disp in yellow %7.4f _b[_cons] " (" %7.4f _se[_cons] ")"
      }
      else {
        qui reg `e' `dvv' `if'`in',   nocons level(`level') `cluster' `hc3'
         disp in green "Coeff. on variance from sampling = " _continue
         disp in yellow %7.4f _b[`dvv'] " (" %7.4f _se[`dvv'] ")"
         disp in green "             Variance of epsilon = " _continue
         disp in yellow %7.4f 0 in blue " (constraint imposed)"
       }
      predict `v' if e(sample)
     regress `depvar' `indvars'  `if'`in' [aw=1/`v'],  `cluster' `hc3' level(`level')
   }
end


*** Analyses

** means

* baseline gender gap

mean base_beta

edvreg base_beta, dvste(base_beta_se) cluster(country_year)

* purged gender gap

mean beta

edvreg beta, dvste(beta_se) cluster(country_year)


** how many items still have statistically significant gender gap?

*baseline gap at 95%

gen base_t05 = base_beta/base_beta_se

gen sig_base_t05 = 0

replace sig_base_t05=1 if base_t05>=1.96

tab sig_base_t05

* baseline gap at 90%

gen base_t10 = base_beta/base_beta_se

gen sig_base_t10= 0

replace sig_base_t10=1 if base_t10>=1.645

tab sig_base_t10

* purged gap at 95%

gen t05 = beta/beta_se

gen sig_t05=0

replace sig_t05=1 if t05>=1.96

tab sig_t05

*significant gap in at least an item in 107 countries (at least 2 in 95 countries)

* purged gap at 90%

gen t10 = beta/beta_se

gen sig_t10=0

replace sig_t10=1 if t10>=1.645

tab sig_t10


***** Models for main beta

* Borjas

edvreg beta prop_parl prop_cabinet head gdppc01 freehouse_inv01 prop_system quota suffrage01 woman man foreign international_org mem_number multiplec truefalse opin quality01 online mailback telephone year2 year3 year4 year5 year6 year7 year8 year9 year10 year11 survey2 survey3 survey4 survey5 survey6 survey7 survey8 survey9 survey10, dvste(beta_se) cluster(country_year)

summarize beta beta_se base_beta base_beta_se prop_parl prop_cabinet head gdppc01 freehouse_inv01 prop_system quota suffrage01 woman man foreign international_org mem_number multiplec truefalse opin quality01 online mailback telephone if e(sample)==1

eststo

estout, style(tex) cells(b(star fmt(2)) se(fmt(2) par)) stats(r2 n) starlevels(* 0.05)

eststo clear


***** Models for baseline beta

* Borjas

edvreg base_beta prop_parl prop_cabinet head gdppc01 freehouse_inv01 prop_system quota suffrage01 woman man foreign international_org mem_number multiplec truefalse opin quality01 online mailback telephone year2 year3 year4 year5 year6 year7 year8 year9 year10 year11 survey2 survey3 survey4 survey5 survey6 survey7 survey8 survey9 survey10, dvste(base_beta_se) cluster(country_year)

* model for DKs

edvreg beta_m_dk prop_parl prop_cabinet head gdppc01 freehouse_inv01 prop_system quota suffrage01 woman man foreign international_org mem_number multiplec truefalse opin quality01 online mailback telephone year2 year3 year4 year5 year6 year7 year8 year9 year10 year11 survey2 survey3 survey4 survey5 survey6 survey7 survey8 survey9 survey10, dvste(beta_m_dk_se) cluster(country_year)

* model for incorrect

edvreg beta_m_inc prop_parl prop_cabinet head gdppc01 freehouse_inv01 prop_system quota suffrage01 woman man foreign international_org mem_number multiplec truefalse opin quality01 online mailback telephone year2 year3 year4 year5 year6 year7 year8 year9 year10 year11 survey2 survey3 survey4 survey5 survey6 survey7 survey8 survey9 survey10, dvste(beta_m_inc_se) cluster(country_year)


******* ADDITIONAL

*** Interactions

* interactions between quotas and indicators of gendered political context

gen quota_propp = quota*prop_parl

gen quota_woman = quota*woman

gen quota_man = quota*man

* interactions between female representation and type of item

gen propp_woman = prop_parl*woman

gen propp_man = prop_parl*man

* quota and prop_parl

edvreg beta prop_parl quota quota_propp prop_cabinet head gdppc01 freehouse_inv01 prop_system suffrage01 woman man foreign international_org mem_number multiplec truefalse opin quality01 year2 year3 year4 year5 year6 year7 year8 year9 year10 year11 survey2 survey3 survey4 survey5 survey6 survey7 survey8 survey9 survey10, dvste(beta_se) cluster(country_year)

* quota and gendered items

edvreg beta woman man quota quota_woman quota_man prop_parl prop_cabinet head gdppc01 freehouse_inv01 prop_system suffrage01 foreign international_org mem_number multiplec truefalse opin quality01 year2 year3 year4 year5 year6 year7 year8 year9 year10 year11 survey2 survey3 survey4 survey5 survey6 survey7 survey8 survey9 survey10, dvste(beta_se) cluster(country_year)

* prop_parl and gendered items

edvreg beta woman man prop_parl propp_woman propp_man prop_cabinet head gdppc01 freehouse_inv01 prop_system suffrage01 quota foreign international_org mem_number multiplec truefalse opin quality01 year2 year3 year4 year5 year6 year7 year8 year9 year10 year11 survey2 survey3 survey4 survey5 survey6 survey7 survey8 survey9 survey10, dvste(beta_se) cluster(country_year)


* interaction with level of democracy

gen propparl_dem = prop_parl*freehouse_inv01

gen propcab_dem =  prop_cabinet*freehouse_inv01

gen head_dem = head*freehouse_inv01

gen man_dem = man*freehouse_inv01

gen woman_dem = woman*freehouse_inv01


edvreg beta prop_parl propparl_dem prop_cabinet propcab_dem head head_dem woman woman_dem man man_dem gdppc01 freehouse_inv01 prop_system quota suffrage01 foreign international_org mem_number multiplec truefalse opin online mailback telephone quality01 year2 year3 year4 year5 year6 year7 year8 year9 year10 year11 survey2 survey3 survey4 survey5 survey6 survey7 survey8 survey9 survey10, dvste(beta_se) cluster(country_year)

* correlation between item with woman and  women�s descriptove representation

pwcorr woman man prop_parl prop_cabinet head, sig
